package com.ruoyi.api.mobileDept;

import com.ruoyi.api.apkVersion.entity.PageData;
import com.ruoyi.api.apkVersion.service.ApkVersionService;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.result.ResultView;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.SysRole;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.*;

@EnableAsync
@Slf4j
@Controller
public class MobileDeptController {
  @Autowired
  private ISysDeptService deptService;
  @Autowired
  private ISysUserService userService;
  @Autowired
  private ISysPostService postService;

  @GetMapping("/api/dept/deptTree")
  @ResponseBody
  public ResultView departTree()
  {
    List<Ztree> ztrees = deptService.selectDeptTree(new SysDept());
    return ResultView.ok(ztrees);
  }
  @PostMapping("/api/dept/deptUsers")
  @ResponseBody
  public ResultView departUsers(@RequestBody SysUser user)
  {
    List<SysUser> list = userService.selectUserList(user);
    return ResultView.ok(list);
  }

  @GetMapping("/api/system/postList")
  @ResponseBody
  public ResultView postList()
  {
    return ResultView.ok(postService.selectPostByGtePostId(1L));
  }

  @Value("${org.superRoleIds}")
  private List<Long> superRoleIds;
  @Value("${org.deptLeaderIds}")
  private List<Long> deptLeaderIds;

  /**
   *返回下以及的组织架构，以及组织架构下的人员总数
   */
  @GetMapping("/api/dept/deptTreeAndUsers/{uid}")
  @ResponseBody
  public ResultView deptTreeAndUsers(@PathVariable @NotNull Long uid,Long deptId)
  {
    List<Long> ssuperTmp1=new ArrayList<>();
    List<Long> deptTmp1=new ArrayList<>();
    ssuperTmp1.addAll(superRoleIds);
    deptTmp1.addAll(deptLeaderIds);
    //根据用户角色来返回组织架构，不同角色的人看到的组织架构不一样
    //如果不是平台、财务、总部窗，总办，部门领导只能查看自己部门的
    //其他人都只能看到自己的孩子节点的组织架构
    SysUser sysUser = userService.selectUserById(uid);
    List<Long>roleIds=new ArrayList<>();
    int roleState=0;
    if(sysUser.getRoles()!=null){
      for(SysRole sysRole:sysUser.getRoles()){
        roleIds.add(sysRole.getRoleId());
      }
    }
    ssuperTmp1.retainAll(roleIds);
    deptTmp1.retainAll(roleIds);
   if(ssuperTmp1.size()>0){
      roleState=1;
    }else if(deptTmp1.size()>0){
      roleState=2;
   }

   if(roleState !=1 && roleState!=2){
     SysDept sysDept=new SysDept();
     SysDept tmp = deptService.selectDeptById(sysUser.getDeptId());
     sysDept.setDeptName(tmp.getDeptName());
     deptService.fadeDepartmentsOnlyChild(sysDept,sysUser);
     return ResultView.ok(sysDept);
   }

    if(roleState == 1){
      if(deptId== null){
        List<SysDept> sysDepts = deptService.selectChildrenByParentId(0L);
        if(sysDepts!=null){
          deptId=sysDepts.get(0).getDeptId();
        }else{
          deptId=100L;
        }
      }
    }else if(roleState == 2){
      if(deptId== null ||deptId ==100){
        deptId=sysUser.getDeptId();
      }
    }
    SysDept sysDept=new SysDept();
    sysDept.setDeptId(deptId);
    deptService.departsAndUsers(sysDept);
    return ResultView.ok(sysDept);
  }
  /**
   *返回下以及的组织架构，以及组织架构下的人员总数
   */
  @GetMapping("/api/dept/deptTreeAndUsers")
  @ResponseBody
  public ResultView deptTreeAndUsers_old(Long deptId)
  {
    if(deptId== null){
      List<SysDept> sysDepts = deptService.selectChildrenByParentId(0L);
      if(sysDepts!=null){
        deptId=sysDepts.get(0).getDeptId();
      }else{
        deptId=100L;
      }
    }
    SysDept sysDept=new SysDept();
    sysDept.setDeptId(deptId);
    deptService.departsAndUsers(sysDept);
    return ResultView.ok(sysDept);
  }

}


